// 用户信息store
import { defineStore } from 'pinia'
import { reactive, ref } from 'vue'

interface userInfoInterFace {
    id: string;
    phone_number: string;
    avatar_url: string;
    user_type: number;
    nickname: string;
    email: string;
    gender: number;
    birth_date?: any;
    province?: any;
    city?: any;
    district?: any;
    address?: any;
    points: number;
    bio?: any;
    created_at: string;
    updated_at: string;
}
interface loginStatusInterFace {
    state: boolean
}

export const useUserInfoStore = defineStore('user', () => {
    const userInfo: userInfoInterFace = ref(JSON.parse(localStorage.getItem('userInfo') as string) || {})
    const loginStatus: loginStatusInterFace = reactive(JSON.parse(localStorage.getItem('loginStatus') as string) || {})
    const token = ref(JSON.parse(localStorage.getItem('token') as string) || '')
    function setLoginStatue(data: loginStatusInterFace): void {
        loginStatus.state = data.state
        localStorage.setItem('loginStatus', JSON.stringify(data))
    }
    function setUserInfo(data: userInfoInterFace): void {
        userInfo.value = data
        localStorage.setItem('userInfo', JSON.stringify(data))
    }
    function setToken(data:string): void {
        token.value = data
        localStorage.setItem('token', JSON.stringify(data))
    }
    function log_out() {
        localStorage.clear()
        token.value = ''
    }
    return { userInfo, loginStatus, log_out, setLoginStatue, setUserInfo ,token,setToken}
})